package control;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import dto.*;
import service.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Islam
 */
public class ChatRoomImp extends UnicastRemoteObject implements ChatRoomInt {

    private static int chatRoomID;
    private Vector<Account> chatRoomMembers = new Vector<>();

    public ChatRoomImp() throws RemoteException {
        chatRoomID++;
    }

    @Override
    public void removeMemberFromRoom(Account account, ChatRoomInt chatRoom) throws RemoteException {
        chatRoom.getChatRoomMembers().remove(account);
        notifyLeavingUser(account, chatRoom);
    }

    @Override
    public void addMemberToRoom(Account account) throws RemoteException{
        getChatRoomMembers().add(account);
       
    }

    @Override
    public void notifyLeavingUser(Account account, ChatRoomInt chatRoom) throws RemoteException {
//        serverRef.getServerImp().getClients()
//        if(serverRef!=null)
//        serverRef.getChatClientRef().updateMembersList(account, chatRoom);
    }
    
    /**
     * @return the ID
     */
    /**
     * @param ID the ID to set
     */
    @Override
    public File download_file(String fileName) //to download file when user request this by
    {
        //File fromserver=new File("E:/Chatting/waelelkholy/RMIChatServer/uploadedfiles/"+fileName);   
        File fromserver = new File("//uploadedfiles/" + fileName);
        return fromserver;
    }

    @Override
    public void upload_file(File upfile) {
        try {
            // Check command-line parameter usage
            File sourceFile = new File(upfile.getAbsolutePath());
            // Check whether source file exists
            if (!sourceFile.exists()) {
                System.out.println("Source file is not exist");
                System.exit(0);
            }
            // Check whether target file exists
            File targetFile = new File("uploadedfiles\\" + sourceFile.getName());
            if (targetFile.exists()) {
                System.out.println("Target file is already exists");
            } else {
                BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(targetFile));
                BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile));

                int r;
                int numberOfBytesCopied = 0;
                while ((r = input.read()) != -1) {
                    output.write((byte) r);
                    numberOfBytesCopied++;
                }
                input.close();
                output.close();
                // Display the file size
                System.out.println(numberOfBytesCopied + " bytes copied");
            }
        } catch (IOException ex) {
            Logger.getLogger(SessionImp.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @return the chatRoomAccounts
     */
    /**
     * @param chatRoomAccounts the chatRoomAccounts to set
     */
    /**
     * @return the chatRoomID
     */
    @Override
    public int getChatRoomID() {
        return chatRoomID;
    }

    /**
     * @param chatRoomID the chatRoomID to set
     */
    @Override
    public void setChatRoomID(int chatRoomID) {
        ChatRoomImp.chatRoomID = chatRoomID;
    }

    /**
     * @return the chatRoomMembers
     */
    @Override
    public Vector<Account> getChatRoomMembers() {
        return chatRoomMembers;
    }

    /**
     * @param chatRoomMembers the chatRoomMembers to set
     */
    public void setChatRoomMembers(Vector<Account> chatRoomMembers) {
        this.chatRoomMembers = chatRoomMembers;
    }
}
